home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / to_stream.cx < prev   
Encoding:
Text File  |  1993-03-19  |  3.8 KB  |  138 lines

  1. /*
  2.  * Copyright (c) 1992    Jin Guojun
  3.  *
  4.  * Disclaimer:  No guarantees of performance accompany this software,
  5.  * nor is any responsibility assumed on the part of the authors.  All the
  6.  * software has been tested extensively and every effort has been made to
  7.  * insure its reliability.
  8.  *
  9.  * to_stream - convert an image to HIPS-JPEG format
  10.  *
  11.  * usage:    to_stream < idata > oseq
  12.  *
  13.  * to load:    cc -o to_stream to_stream.c -hipsh -lhips -ljpeg
  14.  *
  15.  * AUTHOR:    Jin Guojun - 11/12/92
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <hipl_format.h>
  20. #include "imagedef.h"
  21.  
  22. static Flag_Format flagfmt[] =
  23. {
  24.     {"c", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  25. /*  {"f", {LASTFLAG}, 1, {{PTINT, "30", "frames / sec."}, LASTPARAMETER}}, */
  26.     {"n", {LASTFLAG}, 1, {{PTINT, "99999999", "number frames"}, LASTPARAMETER}},
  27.     {"j", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  28.     {"p", {LASTFLAG}, 0, {{PTINT, "0", "pad to 2**"}, LASTPARAMETER}},
  29.     {"Q", {LASTFLAG}, 1, {{PTINT, "75", "Q-factor"}, LASTPARAMETER}},
  30.     LASTFLAG};
  31.  
  32.  
  33. main(argc, argv)
  34. int    argc;
  35. char**    argv;
  36. {
  37. int    oform, qfact, pflag=-1;
  38. Boolean    jflag, ciflag, ccflag, coflag;
  39. int    rows, cols, frames, f, pad;
  40. struct header    hd;
  41. sframe_header    fhd;
  42. Filename    filename;
  43. FILE    *fp;
  44. byte    *fakec;
  45. extern    U_IMAGE    uimg;
  46.  
  47.     Progname = strsave(*argv);
  48.     parseargs(argc, argv, flagfmt, &coflag, &frames, &jflag, &pflag, &qfact,
  49.     FFONE, &filename);
  50.  
  51.     fp = hfopenr(filename);
  52.     jpeg_uimg_init(fp, HIPS, 0, coflag);
  53.     fread_header(fp, &hd, filename);
  54.     if (frames > hd.num_frame)
  55.     frames = hd.num_frame / hd.numcolor;
  56.     else    hd.num_frame = frames * hd.numcolor;
  57.     ciflag = hjpeg_color(&hd);
  58.     ccflag = coflag & !ciflag;
  59.  
  60.     if (get_sinfo(0, 0))    /* stream in    */
  61.     if ((pad=get_sinfo(1, 0)) == 1)    {    /* compress type for reading */
  62.         if (jflag)    prgmerr('j', "s_jpeg to s_jpeg - Why ?");
  63.         stream_jpeg_rinit(fp, get_sinfo_q(0), ciflag | coflag);
  64.     } else if (!jflag)
  65.         prgmerr(0, "Warning: stream to stream - waste time");
  66.  
  67.     set_stream_param(&hd, get_stream_header_sline(hd.ocols, hips_channels(&hd)),
  68.         jflag, -1, qfact, -1 /* no data_size */, 1);
  69.     if (!jflag)
  70.     if (coflag && hd.numcolor == 1)    {
  71.         if (hd.pixel_format != PFRGB)
  72.         hd.sizeimage *= hips_channels(&hd);
  73.         if (ciflag & pad)    { /* jpeg stream in    */
  74.         hd.pixel_format = PFRGB;
  75.         hd.num_frame /= hd.numcolor,    hd.numcolor = 1;
  76.         }
  77.     } else    {
  78.         hd.pixel_format = PFBYTE;
  79.         hd.num_frame /= hd.numcolor,    hd.numcolor = 1;
  80.     }
  81.     write_headeru(&hd, argc, argv);
  82.     if (jflag)
  83.     hhjpeg_winit(stdout, &hd, get_sinfo_q(1), ciflag | coflag);
  84.  
  85.     fakec = (byte *) malloc(hd.ocols * hd.orows * 3);
  86.     if (ccflag)
  87.         hd.image = (byte *) malloc(hd.ocols * hd.orows * 3);
  88.     if (pflag < 0)    pflag = 0;
  89.     if (pflag)    {
  90.         for (pad=0, pflag=(pflag<<1)-1; pflag>>=1; pad++);
  91.         pflag = 1 << pad;
  92.     }
  93.  
  94.     for (f = 0; f < frames; f++) {
  95.     if (stream_jpeg_read_image(fakec, &hd, f) < 1)
  96.         prgmerr('r', "stream_jpeg_read_image");
  97.     if (ccflag) {
  98.         register byte *sp = fakec, *cp = hd.image;
  99.         register int r = hd.orows, w = hd.ocols;
  100.         while (r--) {
  101.             memcpy(cp, sp, w);
  102.             cp += w;
  103.             memcpy(cp, sp, w);
  104.             cp += w;
  105.             memcpy(cp, sp, w);
  106.             cp += w;
  107.             sp += w;
  108.         }
  109.     } else    hd.image = fakec;
  110.  
  111.     if (jflag) {
  112.         if (stream_jpeg_write_frame(hd.image, &fhd) < 0)
  113.         return (perr(HE_WRITEFRFILE, f, filename));
  114.         /*
  115.          * this routine points hd->image to the compressed image, and
  116.          * sets the size of the compressed frame in fhd
  117.          */
  118.         fhd.frame = f;
  119.         if (pflag)    {
  120.         pad = pflag - (fhd.size % pflag);
  121.         fprintf(stderr, "padding by %d bytes \n", pad);
  122.         fhd.size += pad;
  123.         }
  124.         if (write_stream_header(stdout, &hd, &fhd, f) < 0
  125.             || fwrite(hd.image, fhd.size, 1, stdout) != 1)
  126.         return (perr(HE_WRITEFRFILE, f, filename));
  127.  
  128.     } else    write_image(&hd, f);
  129.     }
  130.  
  131. write_jpeg_eof(stdout);
  132. fclose(stdout);
  133. free(fakec);
  134. if (ccflag)
  135.     free(hd.image);
  136. return (0);
  137. }
  138.